<template>
  <div class="app-container">
    <!-- 查询条件开始 -->
    <el-form ref="queryForm" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="字典名称" prop="dictName">
        <el-input
          v-model="queryParams.dictName"
          placeholder="请输入字典名称"
          clearable
          size="small"
          style="width:200px"
        />
      </el-form-item>
      <el-form-item label="字典类型" prop="dictType">
        <el-input
          v-model="queryParams.dictType"
          placeholder="请输入字典类型"
          clearable
          size="small"
          style="width:200px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="字典状态" size="small" style="width:150px">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width:240px"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 查询条件结束 -->

    <!-- 表格工具按钮开始 -->
    <el-row :gutter="10" style="margin-bottom:8px">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          :disabled="multiple"
          size="mini"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-refresh" size="mini" @click="handleCacheAsync">缓存同步</el-button>
      </el-col>
    </el-row>
    <!-- 表格工具按钮结束 -->

    <!-- 数据表格开始 -->
    <el-table
      ref="multipleTable"
      :data="dictTypeTableList"
      tooltip-effect="dark"
      style="width: 100%"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="字典编号" prop="dictId" align="center" />
      <el-table-column label="字典名称" prop="dictName" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="字典类型" prop="dictType" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <router-link :to="'/dict/data/'+scope.row.dictId" class="link-type">
            <span>{{ scope.row.dictType }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" align="center" :formatter="statusFormatter" />
      <el-table-column label="备注" prop="remark" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="创建时间" prop="createTime" align="center" width="180" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-edit"
            size="mini"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            type="text"
            icon="el-icon-delete"
            size="mini"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 数据表格结束 -->

    <!-- 分页控件开始 -->
    <el-pagination
      v-show="total>0"
      style="margin-top:8px"
      :current-page="queryParams.pageNum"
      :page-sizes="[5, 10, 20, 30]"
      :page-size="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 分页控件结束 -->

    <!-- 添加修改弹出层开始 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      center
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="字典名称" prop="dictName">
          <el-input v-model="form.dictName" placeholder="请输入字典名称" clearable size="small" />
        </el-form-item>
        <el-form-item label="字典类型" prop="dictType">
          <el-input v-model="form.dictType" placeholder="请输入字典类型" clearable size="small" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              :value="dict.dictValue"
            >{{ dict.dictLabel }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入字典名称" clearable size="small" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加修改弹出层结束 -->
  </div>
</template>
<script>
// 引入api
import {
  listForPage,
  addDictType,
  updateDictType,
  getDictTypeById,
  deleteDictTypeByIds,
  dictCacheAsync
} from '@/api/system/dict/type'
export default {
  // 定义页面数据
  data() {
    return {
      // 是否加载完成
      loading: false,
      // 选中id数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 分页数据总条数
      total: 0,
      // 字典表格绑定数据
      dictTypeTableList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1, // 当前页
        pageSize: 10, // 页面大小
        dictName: undefined, // 类型名称
        dictType: undefined, // 类型
        status: undefined // 状态
      },
      // 表单数据
      form: {},
      // 表单校验规则
      rules: {
        dictName: [
          {
            required: true,
            message: '字典名称不能为空',
            trigger: 'blur'
          }
        ],
        dictType: [
          {
            required: true,
            message: '字典类型不能为空',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  // 钩子函数
  created() {
    // 查询表格数据
    this.getDictTypeList()
    // 调用全局的根据字典类型查询字典数据的方法
    this.getDataByType('sys_normal_disable').then(res => {
      this.statusOptions = res.data
    })
  },
  methods: {
    // 查询表格数据
    getDictTypeList() {
      // 打开加载进度条
      this.loading = true
      listForPage(this.addDateRange(this.queryParams, this.dateRange)).then(res => {
        this.dictTypeTableList = res.data
        this.total = res.total
        // 关闭加载进度条
        this.loading = false
      })
    },
    // 查询搜索
    handleQuery() {
      this.getDictTypeList()
    },
    // 查询重置
    resetQuery() {
      // 调用全局的重置表单方法
      this.resetForm('queryForm')
      this.dateRange = []
      this.getDictTypeList()
    },
    // 工具按钮新增点击事件
    handleAdd() {
      this.open = true
      this.title = '字典类型添加'
      this.reset()
    },
    // 工具按钮缓存同步点击事件
    handleCacheAsync() {
      this.loading = true
      dictCacheAsync().then(res => {
        this.loading = false
        this.msgSuccess('缓存同步成功')
      })
    },
    // 表格多选时触发
    handleSelectionChange(selection) {
      // 获取id
      this.ids = selection.map(item => item.dictId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 表格修改事件
    handleUpdate(row) {
      const dictId = row.dictId || this.ids
      // 获取字典类型id
      this.title = '字典类型修改'
      this.reset()
      this.open = true
      // 根据dictId查询字典信息
      getDictTypeById(dictId).then(res => {
        this.form = res.data
      })
    },
    // 表格删除事件
    handleDelete(row) {
      const dictId = row.dictId || this.ids
      this.$confirm('此操作将永久删除该字典类型, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDictTypeByIds(dictId).then(res => {
          this.msgSuccess('删除成功')
          this.getDictTypeList()
        })
      }).catch(() => {
        this.msgError('已取消删除')
      })
    },
    // 分页页面大小改变时触发
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getDictTypeList()
    },
    // 分页跳转页面时触发
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.getDictTypeList()
    },
    // 格式化状态数据
    statusFormatter(row) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    // 弹出框保存
    handleSubmit() {
      // 数据校验
      this.$refs['form'].validate((valid) => {
        if (valid) {
          // 判断用户执行的是修改还是添加操作
          if (this.form.dictId === undefined) {
            // 添加
            this.loading = true
            addDictType(this.form).then(res => {
              this.msgSuccess('添加成功')
              this.loading = false
              this.open = false
              // 刷新
              this.getDictTypeList()
            }).catch(() => {
              this.loading = false
            })
          } else {
            this.loading = true
            // 修改
            updateDictType(this.form).then(res => {
              this.loading = false
              this.open = false
              this.getDictTypeList()
              this.msgSuccess('修改成功')
            }).catch(() => {
              this.loading = false
            })
          }
        }
      })
    },
    // 弹出框取消
    cancel() {
      this.open = false
    },
    // 重置表单信息
    reset() {
      this.form = {
        dictId: undefined,
        dictName: undefined,
        dictType: undefined,
        status: '0',
        remark: undefined
      }
      this.resetForm('form')
    }
  }
}
</script>
